package com.ruoyi.project.note.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.SnowflakeIdWorker;
import com.ruoyi.project.note.domain.Note;
import com.ruoyi.project.note.domain.Vo.NotebookVo;
import com.ruoyi.project.note.mapper.NoteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.note.mapper.NotebookMapper;
import com.ruoyi.project.note.domain.Notebook;
import com.ruoyi.project.note.service.INotebookService;

/**
 * 笔记集Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-10
 */
@Service
public class NotebookServiceImpl implements INotebookService 
{
    @Autowired
    private NotebookMapper notebookMapper;

    @Autowired
    private NoteMapper noteMapper;

    /**
     * 查询笔记集
     * 
     * @param notebookId 笔记集主键
     * @return 笔记集
     */
    @Override
    public Notebook selectNotebookByNotebookId(Long notebookId)
    {
        return notebookMapper.selectNotebookByNotebookId(notebookId);
    }

    /**
     * 查询笔记集列表
     * 
     * @param notebook 笔记集
     * @return 笔记集
     */
    @Override
    public List<Notebook> selectNotebookList(Notebook notebook)
    {
        return notebookMapper.selectNotebookList(notebook);
    }

    @Override
    public List<NotebookVo> selectNotebookVoList(NotebookVo notebook)
    {
        List<NotebookVo> notebooks = notebookMapper.selectNotebookList1(notebook);
        for(NotebookVo item:notebooks){
            Long notebookId = item.getNotebookId();
            List<Note> notes = noteMapper.selectNoteListByNotebookId(notebookId);
            item.setNoteList(notes);
        }
        return notebooks;
    }

    /**
     * 新增笔记集
     * 
     * @param notebook 笔记集
     * @return 结果
     */
    @Override
    public int insertNotebook(Notebook notebook)
    {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        Long id = idWorker.nextId();
        notebook.setNotebookId(id);
        notebook.setCreateTime(DateUtils.getNowDate());
        return notebookMapper.insertNotebook(notebook);
    }

    /**
     * 修改笔记集
     * 
     * @param notebook 笔记集
     * @return 结果
     */
    @Override
    public int updateNotebook(Notebook notebook)
    {
        notebook.setUpdateTime(DateUtils.getNowDate());
        Long notebookId = notebook.getNotebookId();
        String notebookName = notebook.getNotebookName();
        int i=noteMapper.updateNotes(notebookId,notebookName);
        return notebookMapper.updateNotebook(notebook);
    }

    /**
     * 批量删除笔记集
     * 
     * @param notebookIds 需要删除的笔记集主键
     * @return 结果
     */
    @Override
    public int deleteNotebookByNotebookIds(Long[] notebookIds)
    {
        Long notebookId = notebookIds[0];
        int i = noteMapper.updateNotes(notebookId, null);
        return notebookMapper.deleteNotebookByNotebookIds(notebookIds);
    }

    /**
     * 删除笔记集信息
     * 
     * @param notebookId 笔记集主键
     * @return 结果
     */
    @Override
    public int deleteNotebookByNotebookId(Long notebookId)
    {
        return notebookMapper.deleteNotebookByNotebookId(notebookId);
    }
}
